package com.train.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.user.domain.SysUserFollow;
import com.train.user.service.SysUserFollowService;
import com.train.user.mapper.SysUserFollowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* @author HP
* @description 针对表【sys_user_follow(用户关注表)】的数据库操作Service实现
* @createDate 2025-10-24 16:01:41
*/
@Service
public class SysUserFollowServiceImpl extends ServiceImpl<SysUserFollowMapper, SysUserFollow>
    implements SysUserFollowService{

    /**
     * 关注用户
     */
    @Override
    @Transactional
    public boolean followUser(Long userId, Long followUserId) {
        // 不能关注自己
        if (userId.equals(followUserId)) {
            return false;
        }

        // 检查是否已经关注
        LambdaQueryWrapper<SysUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserFollow::getUserId, userId)
                    .eq(SysUserFollow::getFollowUserId, followUserId);
        
        SysUserFollow existingFollow = this.getOne(queryWrapper);
        
        if (existingFollow != null) {
            // 如果之前取消过关注,则恢复关注状态
            if ("1".equals(existingFollow.getStatus())) {
                existingFollow.setStatus("0");
                existingFollow.setUpdateBy(String.valueOf(userId));
                existingFollow.setUpdateTime(new Date());
                return this.updateById(existingFollow);
            }
            // 已经关注过了
            return false;
        }

        // 创建新的关注记录
        SysUserFollow follow = new SysUserFollow();
        follow.setUserId(userId);
        follow.setFollowUserId(followUserId);
        follow.setStatus("0");
        follow.setCreateBy(String.valueOf(userId));
        follow.setCreateTime(new Date());
        
        return this.save(follow);
    }

    /**
     * 取消关注
     */
    @Override
    @Transactional
    public boolean unfollowUser(Long userId, Long followUserId) {
        // 查询关注记录
        LambdaQueryWrapper<SysUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserFollow::getUserId, userId)
                    .eq(SysUserFollow::getFollowUserId, followUserId)
                    .eq(SysUserFollow::getStatus, "0");
        
        SysUserFollow follow = this.getOne(queryWrapper);
        
        if (follow == null) {
            return false;
        }

        // 更新状态为取消关注
        follow.setStatus("1");
        follow.setUpdateBy(String.valueOf(userId));
        follow.setUpdateTime(new Date());
        
        return this.updateById(follow);
    }

    /**
     * 检查是否已关注
     */
    @Override
    public boolean isFollowing(Long userId, Long followUserId) {
        LambdaQueryWrapper<SysUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserFollow::getUserId, userId)
                    .eq(SysUserFollow::getFollowUserId, followUserId)
                    .eq(SysUserFollow::getStatus, "0");
        
        return this.count(queryWrapper) > 0;
    }

    /**
     * 获取关注数量
     */
    @Override
    public long getFollowingCount(Long userId) {
        LambdaQueryWrapper<SysUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserFollow::getUserId, userId)
                    .eq(SysUserFollow::getStatus, "0");
        
        return this.count(queryWrapper);
    }

    /**
     * 获取粉丝数量
     */
    @Override
    public long getFollowerCount(Long userId) {
        LambdaQueryWrapper<SysUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserFollow::getFollowUserId, userId)
                    .eq(SysUserFollow::getStatus, "0");
        
        return this.count(queryWrapper);
    }
}




